#include <cstdio>
#include "TileMap.hpp"
#include "Utility.hpp"

TileMap::TileMap(int mWidth, int mHeight, int tWidth, int tHeight) :
    MAP_WIDTH(mWidth),
    MAP_HEIGHT(mHeight),
    TILE_WIDTH(tWidth),
    TILE_HEIGHT(tHeight)
{
    //ctor
    _Map.resize(MAP_WIDTH * MAP_HEIGHT);

    //_spriteTextures[0].loadFromFile("textures/dirt.jpg");
    //_spriteTextures[1].loadFromFile("textures/grass.jpg");

    /*
    _spriteTexture.loadFromFile("textures/floorTiles.png");
    //int textureTileSize = _spriteTexture.getSize().x / 5;

    for (int i = 0; i < 5; i++)
    {
        _textureRects[i].left = i * 32;
        _textureRects[i].top = 0.0f;
        _textureRects[i].width = 32;
        _textureRects[i].height = 32;

        _tileSprites[i].setTexture(_spriteTexture);
        _tileSprites[i].setTextureRect(_textureRects[i]);
        _tileSprites[i].scale(TILE_WIDTH/16.0f, TILE_HEIGHT/16.0f);
    }*/

    //_tileSprites[0].setTexture(_spriteTextures[0]);
    //_tileSprites[1].setTexture(_spriteTextures[1]);

    //_tileSprites[0].scale(TILE_WIDTH/256.0f, TILE_HEIGHT/256.0f);
    //_tileSprites[1].scale(TILE_WIDTH/256.0f, TILE_HEIGHT/256.0f);

    createTileSheet("textures/floorTiles.png", 32, 32);

    newMap();
}

TileMap::~TileMap()
{
    //dtor
}

void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    for (unsigned int i = 0; i < _Map.size(); i++)
    {
        target.draw(_Map[i]._Sprite);
    }
}

void TileMap::newMap()
{
    init_tiles();
    generate_grass();
}

void TileMap::createTileSheet(std::string fname, unsigned int tWidth, unsigned int tHeight)
{
    _spriteTexture.loadFromFile(fname);
    unsigned int tSizeW, tSizeH;
    tSizeW = _spriteTexture.getSize().x;
    tSizeH = _spriteTexture.getSize().y;

    for (unsigned int r = 0; r < tSizeH/tHeight; r++)
    {
        for (unsigned int c = 0; c < tSizeW/tWidth; c++)
        {
            _tileSprites.push_back(sf::Sprite(_spriteTexture, sf::IntRect(c*tWidth, r*tHeight, tWidth, tHeight)));
            _tileSprites.back().setScale(TILE_WIDTH/((float)tWidth), TILE_HEIGHT/((float)tHeight));
	    printf("%ld\n", _tileSprites.size());
        }
    }

}

void TileMap::init_tiles()
{
    //populate tileMap with tile structs
    for (int x = 0; x < MAP_WIDTH; x++)
    {
        for (int y = 0; y < MAP_HEIGHT; y++)
        {
            int t = (y * MAP_WIDTH) + x;
            //int _Type ID for tile. 0 = Stone, 1 = Wood, 2 = Dirt, 3 = Grass, 4 = Mud
            _Map[t]._Type = 0;
            //set all map tiles to dirt by default??
            _Map[t]._Sprite.setTexture(*(_tileSprites[0].getTexture()));
            _Map[t]._Sprite.setTextureRect(_tileSprites[0].getTextureRect());
            _Map[t]._Sprite.setScale(_tileSprites[0].getScale());
            //_Map[t]._Sprite.setColor(sf::Color(75,150,255));
            //still need to figure out tile positioning
            _Map[t]._Sprite.setPosition(x*TILE_WIDTH, y*TILE_HEIGHT);
        }
    }
}
void TileMap::generate_grass()
{
    for (int i = 0; i < 5; i++)
    {
        int curX = randInt(MAP_WIDTH);
        int curY = randInt(MAP_HEIGHT);
        for (int z = 0; z < 5000; z++)
        {
            _Map[curY * MAP_WIDTH + curX]._Type = i;
            _Map[curY * MAP_WIDTH + curX]._Sprite = _tileSprites[i];
            _Map[curY * MAP_WIDTH + curX]._Sprite.setPosition(curX*TILE_WIDTH, curY*TILE_HEIGHT);
            /* Choose a direction to try next */
            //goDir 0 = up, 1 = left, 2 = down, 3 = right
            int goDir = randInt(4);
            switch (goDir)
            {
                //Try to move UP
            case 0:
                if (curY > 0)
                {
                    curY--;
                }
                break;
                //Try to move LEFT
            case 1:
                if (curX > 0)
                {
                    curX--;
                }
                break;
                //Try to move DOWN
            case 2:
                if (curY < MAP_HEIGHT-1)
                {
                    curY++;
                }
                break;
                //Try to move RIGHT
            case 3:
                if (curX < MAP_WIDTH-1)
                {
                    curX++;
                }
                break;
            }
        }
    }
}
